<?php
/**
 * Created by PhpStorm.
 * User: dx
 * Date: 16/4/1
 * Time: 下午6:09
 */
$service_path = __DIR__ . "/../../../api/";
ob_start();
require_once($service_path . "public/index.php");
ob_end_clean();

echo date('Y-m-d H:i:s',time())." coupon  check cancel started \n";

//$rs = DB::table('coupon_base_info')
//    ->where('status',1)
//    ->where('date_type','0')
//    ->where("begin_date","<=",date("Y-m-d H:i:s",time()))->get();
//
////print_r($rs);exit;
//
//foreach($rs as $_one){
//    DB::update('update coupon_base_info set status = 1 where id = ?', array($_one->id));
//    echo date('Y-m-d H:i:s',time())." id = [".$_one->id."] name = [".mb_convert_encoding($_one->name, "GBK", "UTF-8")."] is open\n";
//}

$rs = DB::table('coupon_base_info')
    ->where('status',1)
    ->where('date_type','0')
    ->where("end_date","<",date("Y-m-d",time()))->limit(5000)->get();
foreach($rs as $_one){
    DB::update('update coupons set status = 6 where coupon_base_info_id = ? and status in(1,2)', array($_one->id));
    echo date('Y-m-d H:i:s',time())." id = [".$_one->id."] name = [".mb_convert_encoding($_one->name, "GBK", "UTF-8")."] is close\n";
}

$rs = DB::table('coupon_base_info')
    ->where('status',1)
    ->where('date_type','1')
    ->limit(5000)
    ->get();

foreach($rs as $_one){
    $get_date_type1 = DB::table('coupons')->where('coupon_base_info_id',$_one->id)->where('status',2)->limit(5000)->get();
    foreach($get_date_type1 as $_g){
        if($_g->end_date != null){ //说明已被领取 直接判断时间是否过期
            if(strtotime($_g->end_date) < strtotime(date("Y-m-d",time()))){
                DB::update('update coupons set status = 6 where  id = ?', array($_g->id));
                echo date('Y-m-d H:i:s',time())." id = [".$_g->id."] coupon_base_info_id = [".$_g->coupon_base_info_id."] is close\n";
            }
        }
    }
}

//查询当天有效的优惠券，把状态改为2
//查询有效的优惠活动
$now_time = date("Y-m-d",time());  //获取当前时间
$now_coupon = DB::table('coupon_base_info')
    ->where('status',1)
    ->where('date_type','0')
    ->where("begin_date","<=",$now_time)
    ->where("end_date",">=",$now_time)
    ->get();

if(!is_null($now_coupon)){
    foreach($now_coupon as $val_coupon){
        $get_coupon = DB::table('coupons')->where('coupon_base_info_id',$val_coupon->id)->where('status',6)->get();
        if(!is_null($get_coupon)){
            foreach($get_coupon as $_coupon){
                if($val_coupon->end_date == $now_time && $val_coupon->begin_date == $now_time){
                    DB::update('update coupons set status = 2 where  id = ?', array($_coupon->id));
                    echo date('Y-m-d H:i:s',time())." id = [".$_coupon->id."] coupon_base_info_id = [".$_coupon->coupon_base_info_id."] is close\n";
                }
            }
        }
    }
}



//未到期处理
//$now_coupon1 = DB::table('coupon_base_info')
//    ->where('status',1)
//    ->where('date_type','0')
//    ->where("begin_date",">",$now_time)
//    ->where("end_date",">",$now_time)
//    ->get();

//if(!is_null($now_coupon1)){
//    foreach($now_coupon1 as $val_coupon1){
//        $get_coupon1 = DB::table('coupons')->where('coupon_base_info_id',$val_coupon1->id)->where('status',2)->get();
//        if(!is_null($get_coupon1)){
//            foreach($get_coupon1 as $_coupon1){
//                if($val_coupon1->end_date > $now_time && $val_coupon1->begin_date > $now_time){
//                    DB::update('update coupons set status = 6 where  id = ?', array($_coupon1->id));
//                    echo date('Y-m-d H:i:s',time())." id = [".$_coupon1->id."] coupon_base_info_id = [".$_coupon1->coupon_base_info_id."] is close\n";
//                }
//            }
//        }
//    }
//}



echo date('Y-m-d H:i:s',time())." coupons  check retry stoped \n";
